import React, { useEffect } from 'react';
import { Card, Select, Form, Row, Col, Input, DatePicker } from 'antd';
import { getAddresses } from '@/services/customer/custom_address_api';
import AddressInput from './AddressInput';
import { TruckPreOrder } from '../../types';
import dayjs from 'dayjs';
import {  useIntl} from "@umijs/max";


const { Option } = Select;
interface AddressSelectionProps {
  form?: any;
  formData: TruckPreOrder;
  setFormData: (prev: any) => void;
}


const AddressSelection: React.FC<AddressSelectionProps> = ({ form, formData, setFormData }) => {
  const intl = useIntl();
  const handleAddressChange = (type: 'shipperAddressDT' | 'deliveryAddressDT', value: string) => {
    setFormData(prev => ({
      ...prev,
      [`${type}`]: value
    }));
  };
  const handleDateChange = (value: any) => {
    console.log("change date:", value);
    const localDateTimeString = dayjs(value).format('YYYY-MM-DD HH:mm');
    // console.log(localDateTimeString);
    setFormData(prev => ({
      ...prev,
      pickReadyDatetime: localDateTimeString
    }));
  };
  const [internalForm] = Form.useForm();
  useEffect(() => {
    if (form) {
      form.internalForms = form.internalForms || [];
      form.internalForms.push(internalForm);
    }
  }, [form]);
  return (
    <Card
      title={intl.formatMessage({ id: "pages.order.addressInfo.title" })}
      style={{ paddingBottom: "30px", marginBottom: "10px" }}
      size="small"
    >
      <Form layout="vertical">
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item
              label={intl.formatMessage({ id: "pages.order.address.pickup" })}
              name="shipperAddressDT"
              rules={[
                {
                  required: true,
                  message: intl.formatMessage({
                    id: "validation.address.pickup.required",
                  }),
                },
              ]}
              initialValue={formData.shipperAddressDT}
            >
              <AddressInput
                value={formData.shipperAddressDT}
                onChange={(v) => handleAddressChange("shipperAddressDT", v)}
              />
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              label={intl.formatMessage({ id: "pages.order.address.delivery" })}
              name="deliveryAddressDT"
              rules={[
                {
                  required: true,
                  message: intl.formatMessage({
                    id: "validation.address.delivery.required",
                  }),
                },
              ]}
              initialValue={formData.deliveryAddressDT}
            >
              <AddressInput
                value={formData.deliveryAddressDT}
                onChange={(v) => handleAddressChange("deliveryAddressDT", v)}
              />
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={16}>
          <Col span={12}>
            <Form.Item
              name="pickReadyDatetime"
              label={intl.formatMessage({
                id: "pages.order.address.pickupTime",
              })}
              initialValue={dayjs(formData.pickReadyDatetime)}
              rules={[
                {
                  required: true,
                  message: intl.formatMessage({
                    id: "validation.pickupTime.required",
                  }),
                },
              ]}
              style={{ marginTop: 30 }}
            >
              <DatePicker
                showTime={{
                  format: "HH:mm",
                  showSecond: false,
                }}
                format="YYYY-MM-DD HH:mm"
                style={{ width: "100%" }}
                onChange={handleDateChange}
                disabledDate={(current) => {
                  return current && current < dayjs().startOf("day");
                }}
              />
            </Form.Item>
          </Col>
          <Col span={12}>{/* 注释掉的参考号部分 */}</Col>
        </Row>
      </Form>
    </Card>
  );
};

export default AddressSelection;